package stack;

import java.util.Stack;
import java.util.stream.Collectors;

/**
 * @author : Rookie
 * @description : 删除字符串相邻重复项
 * @date : 2023/11/6
 */
public class DeleteAdjacentDuplicate {

    public static void main(String[] args) {
        String str = "abbaca";
        String result = delete(str);
        System.out.println(result);
    }

    private static String delete(String str) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (stack.isEmpty() || stack.peek() != c) {
                stack.push(c);
            } else {
                stack.pop();
            }
        }
        return stack.stream().map(String::valueOf).collect(Collectors.joining());
    }
}
